package com.botao.tip.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @author 星时代曹波涛
 */
public class TokenUtil {
    private final static String key = "!@#$%^&*IUYTJ$^DSADSsa2132ADSASDA#^%$^$%^$&DYsaFGGS#@$%$#^";

    public static String createToken(String id, String username, int role) {
        return Jwts.builder()
                .setIssuedAt(new Date())
                .setId(id)
                .setSubject(username)
                .claim("role", role)
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24))
                .signWith(SignatureAlgorithm.HS512, key)
                .compact();//打包
    }

    public static boolean verify(String token) {
        return getTokenClaims(token) != null;
    }

    public static Claims getTokenClaims(String token) {
        try {
            return Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
        } catch (Exception e) {
            return null;
        }
    }
}
